package com.zk.shop.servlet.user;

import com.zk.shop.entity.User;
import com.zk.shop.service.Impl.UserServiceImpl;
import com.zk.shop.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName UpdatePwdServlet
 * @Description TODO
 * @Author 来永刚
 * @Date 2019/10/13 0013上午 11:13
 * @Version 1.0
 **/
@WebServlet("user/UpdatePwdServlet")
public class UpdatePwdServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserService userService = new UserServiceImpl();
        int id = Integer.parseInt(req.getParameter("id"));
        String newPwd = req.getParameter("newPwd");
        String reNewPwd = req.getParameter("reNewPwd");
        User user = new User(id,newPwd);
        if (!newPwd.equals(reNewPwd)) {
            resp.getWriter().print("<script>alert('两次密码输入不同')</script>");
        } else {
            boolean flag = userService.updateUserPwd(user);
            System.out.println(flag);
            if (flag) {
                resp.sendRedirect("../login.html");
            } else {
                resp.sendRedirect("../account.html");
            }
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
